<?php

require_once 'src/core/Vendas.php';
require_once 'src/core/Pets.php';
require_once 'src/core/Clientes.php';


$v = new Vendas();

$error = null;

if (!isset($_POST['idvenda'])) {
    $error['form'] = "não existe";
}

/**
 * Deletar
 */
if (isset($_GET['del'])) {
    $v->setIdvenda($_GET['del']);
    $v->delete();
}

/**
 * Inserir
 */
if (isset($_GET['adicionar'])) {
//    echo "<script>alert('Cliente adicionado com sucesso!');</script>";
    if (sizeof($error) == 0) {
        $v->setObjVenda(null, date('Y-m-d'), date('H:i:s'), 0, $_SESSION['id'], $_POST['idpet'], $_POST['idcliente']);
        $v->insert();
        header("Location: ?s=cvenda&msg=1");
    } else {
        $link_form = "?s=cvenda&adicionar";
        $c = new Clientes();
        $listaCliente = $c->select();
        $p = new Pets();
        $listaPets = $p->select();
        include 'src/view/from_venda.php';
    }
}

/**
 * Finalizar
 */
if (isset($_GET['finalizar'])) {
    $v->setIdvenda($_GET['idvenda']);
    $v->load();
    $v->setObjVenda($v->getIdvenda(), date('Y-m-d'), date('H:i:s'), $_GET['total'], $v->getIdfuncionario(), $v->getIdpet(), $v->getIdcliente());
    $v->update();
    header("Location: ?s=cvenda&msg=2");
}
/**
 * Listar
 */
$lista = $v->selectNomesAbertas('', 'ORDER BY data, hora');
$listaFechadas = $v->selectNomesFechadas('', 'ORDER BY data, hora');

include 'src/view/venda.php';
?>